//
//  Caller.m
//  Cidney
//
//  Created by Chris Karr on 1/12/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <AddressBook/AddressBook.h>

#import "Caller.h"
#import "Dispatcher.h"
#import "FilterManager.h"

#define TAGS_PROPERTY @"net.aetherial.cidney.person.tags"

@implementation Caller

- (id) initWithCapacity: (NSUInteger) numItems
{
	if ([ABPerson typeOfProperty:TAGS_PROPERTY] == kABErrorInProperty)
		[ABPerson addPropertiesAndTypes:[NSDictionary dictionaryWithObjectsAndKeys:
												[NSNumber numberWithInt:kABStringProperty], TAGS_PROPERTY, nil]];

	if ([ABPerson typeOfProperty:SOUND_PROPERTY] == kABErrorInProperty)
		[ABPerson addPropertiesAndTypes:[NSDictionary dictionaryWithObjectsAndKeys:
												[NSNumber numberWithInt:kABStringProperty], SOUND_PROPERTY, nil]];


	dict = [[NSMutableDictionary alloc] initWithCapacity:numItems];
	[dict setValue:@"Unknown Caller" forKey:@"name"];
	[dict setValue:[NSNumber numberWithBool:NO] forKey:@"blacklist"];

	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBlacklist:) name:BLACKLIST_BROADCAST object:nil];
	
	return self;
}

- (void) updateBlacklist:(NSNotification *) msg
{
	NSDictionary * userInfo = [msg userInfo];
	
	NSSet * blacklist = [userInfo valueForKey:BLACKLIST];
	
	NSString * myName = [self valueForKey:@"name"];
	NSString * myNumber = [self valueForKey:@"number"];
	[dict setValue:[NSNumber numberWithBool:NO] forKey:@"blacklist"];
	
	for (NSDictionary * call in blacklist)
	{
		NSString * theirName = [call valueForKey:NAME_KEY];
		NSString * theirNumber = [call valueForKey:NUMBER_KEY];
		
		if ([myName isEqual:theirName] && [myNumber isEqual:theirNumber])
		{
			[self willChangeValueForKey:@"color"];
			[dict setValue:[NSNumber numberWithBool:YES] forKey:@"blacklist"];
			[self didChangeValueForKey:@"color"];
		}
	}
}

- (void) dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[dict release];
	[super dealloc];
}

- (void) setValue:(NSObject *) value forKey:(NSString *) key
{
	if ([key isEqual:PERSON_KEY] && value != nil)
	{
		[dict setValue:value forKey:key];
		
		ABPerson * person = (ABPerson *) value;
		
		NSString * name = [NSString stringWithFormat:@"%@ %@", [person valueForProperty:kABFirstNameProperty], [person valueForProperty:kABLastNameProperty]];

		if ([[person valueForProperty:kABPersonFlags] boolValue])
			name = [person valueForProperty:kABOrganizationProperty];
			
		[dict setValue:name forKey:@"name"];
		[dict setValue:name forKey:NAME_KEY];
	}
	else if ([key isEqualToString:@"tags"])
	{
		ABPerson * person = [self valueForKey:PERSON_KEY];
		
		if (person != nil)
		{
			NSMutableString * tokString = [NSMutableString string];

			for (NSString * token in ((NSArray *) value))
			{
				if ([tokString length] < 1)
					[tokString appendString:[NSString stringWithFormat:@"%@", token]];
				else
					[tokString appendString:[NSString stringWithFormat:@",%@", token]];
			}

			if ([person setValue:tokString forProperty:TAGS_PROPERTY])
				[[ABAddressBook sharedAddressBook] save];
		}
	}
	else if ([key isEqualToString:@"sound"])
	{
		ABPerson * person = [self valueForKey:PERSON_KEY];
		
		if (person != nil)
		{
			if (value == nil && [person removeValueForProperty:SOUND_PROPERTY])
				[[ABAddressBook sharedAddressBook] save];
			if ([person setValue:value forProperty:SOUND_PROPERTY])
				[[ABAddressBook sharedAddressBook] save];
		}
	}
	else
		[dict setValue:value forKey:key];
}

- (id) objectForKey:(NSString *) key
{
	if ([key isEqualToString:@"tags"])
	{
		ABPerson * person = [self valueForKey:PERSON_KEY];
		
		if (person != nil)
			return [person valueForProperty:TAGS_PROPERTY];
		else
			return @"unknown";
	}
	else if ([key isEqualToString:@"sound"])
	{
		ABPerson * person = [self valueForKey:PERSON_KEY];
		
		if (person != nil)
			return [person valueForProperty:SOUND_PROPERTY];
		else
			return nil;
	}
	else if ([key isEqualToString:@"color"])
	{
		if ([[self valueForKey:@"blacklist"] boolValue])
			return [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.5];
		else
			return [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:1.0];
	}
		
	return [dict objectForKey:key];
}

- (NSUInteger) count
{
	return [dict count];
}

- (NSEnumerator *) keyEnumerator
{
	return [dict keyEnumerator];
}
@end
